package com.study.juc;

import java.util.concurrent.locks.ReentrantLock;

public class ReEnterLock {
    private static int t = 50;
    private static ReentrantLock lock = new ReentrantLock();

    public static synchronized void test() {
        if (t < 0) {
            return;
        }
        t--;
        System.out.println(Thread.currentThread().getName()+" ：test ");
        test();
    }

    public static void test2() {
        lock.lock();
        try {
            if (t < 0) {
                return;
            }
            t--;
            System.out.println(Thread.currentThread().getName()+" ：test2 ");
            test2();
        } finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            test();
        }, "aaaa").start();
        new Thread(() -> {
            test2();
        }, "aaaa").start();
        Thread.sleep(3000);
    }
}
